package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderSettingVo;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import cn.itcast.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/ordersetting")
@CrossOrigin
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 查询这个月预约的人数
     * @param date
     * @return
     */
    @GetMapping("/getOrderSettingsByMonth")    //date=获取用户当前所在页的年月份
    public Result queryOrderSetting(@RequestParam(value = "date")String date){
        try {
            Integer year = Integer.valueOf(date.substring(0,4));//截取字符串的年份
            Integer month = Integer.valueOf(date.substring(5));//截取字符串的月份
            //根据年和月，调用service实现类查询mysql，返回该月份中全部日期的预约数和已预约数
            List<OrderSettingVo> orderSettingVos = this.orderSettingService.orderSettingFindAll(year, month);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettingVos);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 保存用户输入的预约数据
     * @param param
     * @return
     */
    @PutMapping("/updateOrderSettingNumber")
    public Result saveOrderSettingNumber(@RequestBody Map<String,String> param){
        try {
            if (param != null){
                this.orderSettingService.saveOrderSetting(param);
                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.ORDERSETTING_FAIL);
    }


    /**
     * 文件上传，并将内容保存到数据库
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile file){
        //读取文件
        try {
            //用工具类解析文件
            List<String[]> fileList = POIUtils.readExcel(file);
            //确保文件不为空
            if (fileList != null && fileList.size() > 0){
                //遍历对象并存入集合
                List<OrderSetting> orderSettings = new ArrayList<>();
                for (String[] strings : fileList) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    orderSettings.add(orderSetting);
                }
                this.orderSettingService.add(orderSettings);
            }
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

}
